import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
    //解决编程式路由往同一地址跳转时会报错的情况

const routes = [
    //一级页面
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        redirect: '/home/film',
        component: () =>
            import ('../views/Home.vue'),
        //二级页面
        children: [
            //电影
            {
                path: 'film',
                name: 'film',
                redirect: '/home/film/showing',

                component: () =>
                    import ('../SecondaryView/film.vue'),
                children: [
                    // 正在热映
                    {
                        path: 'showing',
                        name: 'showing',
                        component: () =>
                            import ('../components/showing.vue')
                    },
                    // 即将上映
                    {
                        path: 'coming',
                        name: 'coming',
                        component: () =>
                            import ('../components/coming.vue')
                    },
                ]
            },
            //影院
            {
                path: 'cinema',
                name: 'cinema',
                component: () =>
                    import ('../SecondaryView/cinema.vue'),
            },
            //资讯
            {
                path: 'info',
                name: 'info',
                component: () =>
                    import ('../SecondaryView/info.vue'),
            },
            //我的
            {
                path: 'my',
                name: 'my',
                component: () =>
                    import ('../SecondaryView/my.vue'),
            }
        ]
    }, {
        path: '/city',
        name: 'City',
        component: () =>
            import ('../views/City.vue')
    },
    {
        path: '/cinemasearch',
        name: 'cinemasearch',
        component: () =>
            import ('../components/cinemasearch.vue'),
    },

    //登录
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('../views/Login.vue')
    },
    // 注册
    {
        path: '/reg',
        name: 'reg',
        component: () =>
            import ('../views/Reg.vue')
    },
    //详情页
    {
        path: '/Detail',
        name: 'Detail',
        component: () =>
            import ('../views/Detail.vue')

    },
    // 电影院
    {
        path: '/Cinema',
        name: 'Cinema',
        component: () =>
            import ('../views/Cinema.vue')


    },
    // 电影院买票
    {
        path: '/cinemas',
        name: 'cinemas',
        component: () =>
            import ('../views/Cinemas.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

router.beforeEach((to, from, next) => {
    if (to.name !== 'reg' && to.name !== 'login' && (window.sessionStorage.getItem("loginData") == null || window.sessionStorage.getItem("loginData") == "")) next('/login')
        // 如果用户未能验证身份，则 `next` 会被调用两次
    next()
})

export default router